package mobile.pubcrawler;

import java.util.ArrayList;
import mobile.pubcrawler.model.DataModel;
import mobile.pubcrawler.model.DataModelLocation;
import android.content.Context;
import android.graphics.Color;
import android.location.Address;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

public class PubTourListAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<DataModelLocation> selectedLocations;
    private ArrayList<DataModelLocation> locationList;
    private static final String TAG = "PubTourListAdapter";
	
	// Mapping-Array, um die Positionen, die die Reihenfolge der Locations innerhalb einer
	// Pubcrawl-Tour angeben, zum Index der Locations im DataModel zuzuordnen
	private int[] mapping; 
	
	public PubTourListAdapter(Context context) {
		this.context = context;
		this.selectedLocations = DataModel.getInstance().getSelectedLocations();
		this.locationList = DataModel.getInstance().getLocationList();
		mapping = new int[selectedLocations.size()+1];
		for (int i=0; i<locationList.size();i++) 
			if (locationList.get(i).isSelected()) 
				mapping[locationList.get(i).getPosition()]=i;
	}
		
	public int getCount(){
		return selectedLocations.size()+1;
	}
	
    public Object getItem(int position) {
        return position;
    }
    
    public long getItemId(int position) {
        return position;
    }
	
    public View getView(int position, View convertView, ViewGroup parent) {
    	
//    	Die Distanz zum jeweils letzten Eintrag in der Liste wird berechnet
    	LocationEntry le;
    	long distance = 0;
    	double d = 0.0;
    	int positionInLocationList = mapping[position];
    	Log.v(TAG, "positionInLocationList: " + positionInLocationList);
    	Log.v(TAG, "position: " + position );
    	if(position == 1)
    		distance = locationList.get(positionInLocationList).getDistance();
    	else if(position > 1){
    	    d = (locationList.get(positionInLocationList).getLocation()).distanceTo((locationList.get(mapping[position-1]).getLocation()));
    	    distance = Math.round(d);
    	}
//    	Log.v(TAG, "Distance: " + distance);   	
    	if (position ==0) 
    		le = new LocationEntry(context, null, position, 0, 0);
    	 else 
    		le = new LocationEntry(context, locationList.get(positionInLocationList), position, positionInLocationList, distance);
    	
    	return le.getView();
    }
    
    private class LocationEntry {
    	
    	private View view;
    
    	
    	public LocationEntry(Context context, DataModelLocation location, int positionInListView, int positionInLocationList, long distanceinm) {
            
	    	final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    	view = inflater.inflate(R.layout.pubtourlistitem, null);         	
	    	
	    	TextView number =  (TextView)view.findViewById(R.id.currentTour_number);
	    	number.setTextSize(36);
	    	number.setTextColor(Color.RED);
	    	
	    	TextView distance =  (TextView)view.findViewById(R.id.currentTour_distance);
	    	distance.setText(String.valueOf(distanceinm)+"m");
	    	distance.setTextSize(12);
	    	
	        TextView description = (TextView)view.findViewById(R.id.currentTour_description); 
	        description.setTextSize(14);
	        
	        TextView address = (TextView)view.findViewById(R.id.currentTour_address); 
	        address.setTextSize(12);
	
	        RatingBar rating = (RatingBar)view.findViewById(R.id.currentTour_small_ratingbar); 
	        rating.setNumStars(5);
	        rating.setIsIndicator(true);
	       
	        if (positionInListView==0) {
	        	ImageView point = (ImageView)view.findViewById(R.id.currentTour_location);
	        	point.setVisibility(View.VISIBLE);
	        	number.setVisibility(View.GONE);
	        	rating.setVisibility(View.GONE);
	        	description.setText(context.getString(R.string.currentTourStartLocation));
	        	Address startAddress = DataModel.getInstance().getStartAddress();
	        	if (startAddress!=null) {
	        		String str = startAddress.getAddressLine(0);
	        		if (startAddress.getMaxAddressLineIndex()>0)  str += "<br>"+startAddress.getAddressLine(1);
	        		address.setText(Html.fromHtml(str));	
	        	}
	        } else {
	        	number.setText(String.valueOf(positionInListView)+".");
	        	description.setText(Html.fromHtml(location.getName()));
		        address.setText(Html.fromHtml(location.getAddress()+"<br>"+location.getPostalCode()+" "+location.getCity()));
		        rating.setRating(location.getOverallRating());     
		        view.setOnClickListener(new detailsListener(positionInLocationList));
	        }
        
    	}
    
	    public View getView() {
	    	return view;
	    }
	    
	    public class detailsListener implements OnClickListener {
	    	int position;
	    	public detailsListener(int pos){
	    		position = pos;
	    	}
	        public void onClick(View v) {
	        	PubCrawlerUI.getInstance().startPubDetails(position);
	        }
	    }
	    
    }
    
}
